package com.tanguy.user.ui.window

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.tanguy.user.App
import com.tanguy.user.App.Companion.app
import com.tanguy.user.R
import com.tanguy.user.base.BasePopupWindow
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.utlis.extend.click
import com.tanguy.user.utlis.extend.getEditTextString
import com.tanguy.user.utlis.extend.requestSucceed
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/11/20 0020 09:58
 * type=1 修改名字
 * type=2 修改签名
 */
class UpMassageWindow(context: Context, val type: Int, val content: String, private val sureClick: (msg: String) -> Unit) : BasePopupWindow(context) {
    override fun setContentRes(): Int = R.layout.window_up_message
    private lateinit var mTvTitle: TextView
    private lateinit var mEtName: EditText
    private lateinit var mEtSignature: EditText

    override fun initView(view: View) {
        super.initView(view)
        view.apply {
            click(R.id.iv_close) {
                dismiss()
            }
            click(R.id.tv_next) {
                upMessage()
            }
            mTvTitle = find(R.id.tv_title)
            mEtName = find(R.id.et_content_name)
            mEtSignature = find(R.id.et_content_signature)
        }
    }

    private fun upMessage() {
        if (type == 1) {
            val name = getEditTextString(mEtName)
            if (TextUtils.isEmpty(name)) {
                showToast("姓名不能为空!")
                return
            }
            if (name == app.user.name) {
                sureClick.invoke(name)
                dismiss()
            } else {
                updateInfo(name = name) {
                    sureClick.invoke(name)
                    app.user.name = name
                    dismiss()
                }
            }
        } else {
            val signature = getEditTextString(mEtSignature)
            if (TextUtils.isEmpty(signature)) {
                showToast("签名不能为空!")
                return
            }
            if (signature == app.user.autograph) {
                sureClick.invoke(signature)
                dismiss()
            } else {
                updateInfo(autograph = signature) {
                    sureClick.invoke(signature)
                    app.user.autograph = signature
                    dismiss()
                }
            }
        }

    }

    override fun initEvent() {
        super.initEvent()
        if (type == 1) {
            mTvTitle.text = "修改姓名"
            mEtName.setText(content)
            mEtName.setSelection(content.length)
            mEtName.visibility = View.VISIBLE
            mEtSignature.visibility = View.GONE
        } else {
            mTvTitle.text = "修改签名"
            mEtSignature.setText(content)
            mEtSignature.setSelection(content.length)
            mEtName.visibility = View.GONE
            mEtSignature.visibility = View.VISIBLE
        }
    }

    /**
     * 修改个人信息
     */
    fun updateInfo(headImg: String = "", name: String = "", autograph: String = "", sex: Int? = null, succeed: () -> Unit) {
        HttpManager.updateInfo(App.app.user.userId, headImg, name, autograph, sex)
                .requestSucceed(this) {
                    succeed.invoke()
                }

    }
}